package cn.itcast.work.controller.commit;


import cn.itcast.work.pojo.Commit;
import cn.itcast.work.pojo.Recover;
import cn.itcast.work.pojo.Text;
import cn.itcast.work.pojo.User;
import cn.itcast.work.service.commit_service.CommitService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * * @author: Mao Wanting
 * * @date: 2024/6/16
 * *description:评论的控制器
 **/
@Slf4j
@Controller
@RequestMapping("/CommitController")
public class CommitController {

    @Resource(name = "CommitService")
    private CommitService commitService;

    @RequestMapping("/showCommits")
    @ResponseBody
    public void showCommit(HttpSession httpSession, Text text) {
        log.info("[controller]:执行查询文章评论操作");

        httpSession.setAttribute("article_id", text.getId());//得到文章的id,
        //查找该文章的所有评论和回复
        Map<Commit, List<Recover>> map;
        map = commitService.selectCommitAndRecover(text);
        httpSession.setAttribute("commit_recover_map", map);
    }


    //1.添加评论
    //访问当前网页的article_id
    @RequestMapping("/addCommit")
    @ResponseBody
    public Object addCommit(@RequestParam("commit") String commit,
                            HttpSession httpSession) {

        log.info("[controller]:执行添加评论操作");

        System.out.println("新添加的commit = " + commit);
        //直接从session里面拿username，不需要再传值了
        User user = (User) httpSession.getAttribute("user");

        Text text = new Text();
        text.setId((Integer) httpSession.getAttribute("article_id"));


        //传递的内容
        Commit commit1 = new Commit();
        commit1.setTime(new Date());
        commit1.setContent(commit);
        String result;
        result = commitService.addCommit(user, text, commit1);
        Map<String, String> JsonStr = new HashMap<>();
        JsonStr.put("result", result);
        return JsonStr;
    }


    //2.添加回复
    @RequestMapping("/addRecover")
    @ResponseBody
    public Object addRecover(HttpSession httpSession,
                             @RequestParam("commit_time") String commit,
                             @RequestParam("recover_content") String recover) {
        log.info("[controller]:用户添加回复操作");
        User user = (User) httpSession.getAttribute("user");

        //前端传递
        Recover recover1 = new Recover();
        recover1.setTime(new Date());
        recover1.setContent(recover);


        Commit commit1 = new Commit();
        commit1.setTimeString(commit);//需要日期

        String result;
        result = commitService.addRecover(user, commit1, recover1);
        Map<String, String> JsonStr = new HashMap<>();
        JsonStr.put("result", result);
        return JsonStr;

    }


    //3.删除评论
    @RequestMapping("/deleteCommit")
    @ResponseBody
    public Object deleteCommit(HttpSession httpSession,
                               @RequestParam("commit_time") String commit_time,
                               @RequestParam("commit_content") String commit_content) {

        log.info("[controller]:用户删除评论操作");
        User user = (User) httpSession.getAttribute("user");

        Commit commit = new Commit();
        commit.setContent(commit_content);
        commit.setTimeString(commit_time);

        String result;
        result = commitService.deleteCommit(user, commit);
        Map<String, String> JsonStr = new HashMap<>();
        JsonStr.put("result", result);
        return JsonStr;
    }

    //4.删除回复
    @RequestMapping("/deleteRecover")
    @ResponseBody
    public Object deleteRecover(HttpSession httpSession,
                                @RequestParam("commit_time") String commit_time,
                                @RequestParam("recover_time") String recover_time,
                                @RequestParam("recover_content") String recover_content) {
        log.info("[controller]:用户删除回复操作");
        User user = (User) httpSession.getAttribute("user");

        Commit commit = new Commit();
        commit.setTimeString(commit_time);//时间字符串

        Recover recover = new Recover();
        recover.setTimeString(recover_time);
        recover.setContent(recover_content);

        String result;
        result = commitService.deleteRecover(user, commit, recover);
        Map<String, String> JsonStr = new HashMap<>();
        JsonStr.put("result", result);
        return JsonStr;
    }

    //强制删除评论
    @RequestMapping("/DeleteCommit")
    @ResponseBody
    public Object DeleteCommit(HttpSession httpSession,
                               @RequestParam("commit_time") String commit_time,
                               @RequestParam("commit_content") String commit_content,
                               @RequestParam("commit_id") String commit_id) {
        log.info("[controller]:作者强制删除评论操作");
        User user = (User) httpSession.getAttribute("user");

        System.out.println(commit_id);
        int commitid = Integer.parseInt(commit_id);

        Commit commit = new Commit();

        commit.setContent(commit_content);
        commit.setTimeString(commit_time);
        commit.setId(commitid);

        String result;
        result = commitService.DeleteCommit(commit);
        Map<String, String> JsonStr = new HashMap<>();
        JsonStr.put("result", result);
        return JsonStr;

    }


    //强制删除回复
    @RequestMapping("/DeleteRecover")
    @ResponseBody
    public Object DeleteRecover(HttpSession httpSession,
                                @RequestParam("commit_time") String commit_time,
                                @RequestParam("recover_time") String recover_time,
                                @RequestParam("recover_content") String recover_content) {
        log.info("[controller]:作者强制删除回复操作");

        Commit commit = new Commit();
        commit.setTimeString(commit_time);//时间字符串

        Recover recover = new Recover();
        recover.setTimeString(recover_time);
        recover.setContent(recover_content);

        String result;
        result = commitService.DeleteRecover(recover, commit);
        Map<String, String> JsonStr = new HashMap<>();
        JsonStr.put("result", result);
        return JsonStr;
    }
}
